<?php
class BaseModel extends Zend_Db_Table_Abstract {

    protected $_name;
    protected $_rowClass; // <== THIS IS REALLY HELPFUL
    protected $_configs; // <== THIS IS REALLY HELPFUL
    protected $_operationOptions;
    protected $_selectObject;
    protected $_logged;
    protected $_cache;
    protected $_quantidade_paginacao;
    protected $_page = 0;
	protected $_pagevars = array();
	protected $_site;
	
	public function init(){
		parent::init();
		$this->_configs = Zend_Registry::get('configs');

		//CACHE
		$this->_cache = Zend_Registry::get('cache');

		//LOAD SITE INFOS
		if( Zend_Registry::isRegistered('site') )
			$this->_site = Zend_Registry::get('site');

		$auth = Zend_Auth::getInstance();
		$this->_logged = $auth->getStorage()->read();
	}

	/**
	 * @return the $_name
	 */
	public function getName() {
		return $this->_name;
	}

	public function getDb(){
		return $this->_db;
	}
	
	public function addPageVars($index, $value ){
		$this->_pagevars[$index] = $value;
	}

	public function getPageVars(){
		return $this->_pagevars;
	}
	
    public function setOperationId( $options ) {
    	$this->_operationOptions = array(
    		'cliente_id'=>$options['cliente_id'],
    		'site_id'=>$options['site_id'],
    		'operacao'=>$options['operacao'],
    		'pagina'=> $options['operacao'],
    		'tabelas'=> $options['tabelas']
    	);
    }

    public function getOperationId(){
    	return $this->_operationOptions;
    }

    public function unsetOperationId(){
    	$this->_operationOptions = NULL;
    }

	function setSelectObject( $select ){
    	$this->_selectObject = $select;
    }

    function unsetSelectObject( ){
    	$this->_selectObject = NULL;
    }
    
    
    function getSelectObject(){
    	if( $this->_selectObject == NULL )
    		$this->_selectObject = $this->select();
    		
    	return $this->_selectObject;
    }

	/*
	 * PAGE OPTIONS
	 */
    public function setPage( $page ){
    	$this->_page = $page;
    }

    public function getPage(){
    	return $this->_page;
    }

	public function unsetPage( ){
    	unset( $this->_page );
    }

    /*
     * PAGINACAO OPTIONS
     */
    public function setQuantidadePaginacao( $quantidade_paginacao ){
		$this->_quantidade_paginacao = $quantidade_paginacao;		
	}

	public function getQuantidadePaginacao(){
		if( !$this->_quantidade_paginacao )
			$this->_quantidade_paginacao = $this->_configs['third']['quantidade_paginacao'];

		return $this->_quantidade_paginacao;		
	}

	function getAll(){
		//RETURN OPERATION ID
		$operationId = $this->getOperationId();

			//GET DATA
    		$select = $this->getSelectObject();

    		$data = $this->fetchAll($select); 
    		if( $data )
    			$data = $data->toArray();

        //DESSETA ID DA OPERACAO
        $this->unsetOperationId();
		$this->unsetPage();

		return $data;
	}
	
	function getAllWithPaginator( ){
		//RETURN OPERATION ID
		$operationId = $this->getOperationId();

			//GET DATA
    		$data = $this->getSelectObject();

    		$paginator = Third_Paginator::factory( $data );
          	$paginator->setItemCountPerPage( $this->getQuantidadePaginacao() );
          	$paginator->setCurrentPageNumber( $this->getPage() );

		    Third_Paginator::setDefaultScrollingStyle('Sliding');
		      
        //DESSETA ID DA OPERACAO
        $this->unsetOperationId();
		$this->unsetPage();

		return $paginator;
	}

	function getAllBySite( $sites_id = NULL ){
		//ID DO SITE
		if( !$sites_id )
			$sites_id = $this->_logged->sites_id;
		
		//OBJECT
		$select = $this->getSelectObject();
		$select->where( 'sites_id = ?', $sites_id )
			   ->where( 'status != ?', '2' );

		$this->setSelectObject( $select );

		return $this->getAllWithPaginator( );
	}
	
	function getById( $id, $pKey = "id", $sites_id = NULL ){
		//ID DO SITE
		if( !$sites_id )
			$sites_id = $this->_logged->sites_id;
		
		//OBJECT
		$select = $this->getSelectObject();
		$select->where( 'sites_id = ?', $sites_id )
			   ->where( 'status != ?', '2' )
			   ->where( $pKey.' = ?', $id );

		$select->order(array($pKey.' DESC'));
		
		$this->setSelectObject( $select );
		$select = $this->getSelectObject(); 
		
		$data = $this->fetchAll( $select );

		$objData = NULL;
		if( $data->count() )
			$objData = $data->current()->toArray();

		return $objData;
	}
}

